package com.bawei.day0129.view.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.bawei.day0129.Bean.YzmBean;
import com.bawei.day0129.Bean.ZhuCheBean;
import com.bawei.day0129.R;
import com.bawei.day0129.base.BaseActivity;
import com.bawei.day0129.databinding.ActivityZhucBinding;
import com.bawei.day0129.utils.RsaCoder;
import com.bawei.day0129.viewmodel.ZhuCheViewModel;
import com.bawei.day0129.viewmodel.ZsViewModel;

public class ZhucActivity extends BaseActivity<ZhuCheViewModel, ActivityZhucBinding> {
    private int i = 60;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what==1){
                i--;
                dataBinding.huoquyzm.setText(i+"s");
                if (i==0){
                    dataBinding.huoquyzm.setText("重新发送");
                    dataBinding.huoquyzm.setClickable(true);
                    handler.removeCallbacksAndMessages(null);
                }
                handler.sendEmptyMessageDelayed(1,1000);
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_zhuc;
    }

    @Override
    protected void initView() {
        dataBinding.huoquyzm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = dataBinding.zhuchename.getText().toString();
                String titles="^\\w+({-+.]\\w+)*@qq.com";
                if (TextUtils.isEmpty(s)){
                    Toast.makeText(ZhucActivity.this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!s.matches(titles)){
                    Toast.makeText(ZhucActivity.this, "邮箱格式不对", Toast.LENGTH_SHORT).show();
                    return;
                }
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        handler.sendEmptyMessageDelayed(1,1000);
                        dataBinding.huoquyzm.setClickable(false);
                        viewModel.getGetYzm(s).observe(ZhucActivity.this, new Observer<YzmBean>() {
                            @Override
                            public void onChanged(YzmBean yzmBean) {
                                Toast.makeText(ZhucActivity.this, yzmBean.getMessage(), Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
                dataBinding.zhuchebut.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final String s = dataBinding.zhuchename.getText().toString();
                        final String s3 = dataBinding.yzm.getText().toString();
                        final String s1 = dataBinding.zhuchepwd.getText().toString();
                        final String s2 = dataBinding.zhuchepwds.getText().toString();
                        String emailzz = "^\\w+([-+.]\\w+)*@qq.com$";
                        if(TextUtils.isEmpty(s)){
                            Toast.makeText(ZhucActivity.this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if(TextUtils.isEmpty(s1)){
                            Toast.makeText(ZhucActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if(TextUtils.isEmpty(s3)){
                            Toast.makeText(ZhucActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if(TextUtils.isEmpty(s2)){
                            Toast.makeText(ZhucActivity.this, "确认密码不能为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (!s1.equals(s2)){
                            Toast.makeText(ZhucActivity.this, "请确认密码是否一致", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (!s.matches(emailzz)){
                            Toast.makeText(ZhucActivity.this, "邮箱格式有误", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        try {
                            final String s4 = RsaCoder.encryptByPublicKey(s1);
                            viewModel.getGetZhuChe(s,s3,s4,s4,"").observe(ZhucActivity.this, new Observer<ZhuCheBean>() {
                                @Override
                                public void onChanged(ZhuCheBean zhuCheBean) {
                                    Toast.makeText(ZhucActivity.this, zhuCheBean.getMessage(), Toast.LENGTH_SHORT).show();
                                    if(zhuCheBean.getStatus().equals("0000")){
                                        finish();
                                    }
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}